package com.wizz.trade.trading.controller;

import com.wizz.trade.common.PageVo;
import com.wizz.trade.common.ResponseResult;
import com.wizz.trade.common.constant.RequestQueryTypeConstant;
import com.wizz.trade.common.constant.StatusCode;
import com.wizz.trade.trading.dto.FpositionDto;
import com.wizz.trade.trading.dto.TradeRequestDto;
import com.wizz.trade.trading.dto.page.RequestPageDto;
import com.wizz.trade.trading.dto.page.TradePageDto;
import com.wizz.trade.trading.service.RequestService;
import com.wizz.trade.trading.service.TradeService;
import com.wizz.trade.trading.vo.TradeForSecondPageVo;
import com.wizz.trade.trading.vo.TradeForThirdPageVo;
import com.wizz.trade.trading.vo.basic.RequestVo;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * 贸易模块/采购订单付款申请
 *
 * @author fyftless
 */
@RestController
@RequestMapping("/purchase")
public class TradeThirdController {

    @Resource
    private RequestService requestService;

    @Resource
    private TradeService tradeService;

    /**
     * 生成采购付款申请
     * @param tradeRequestDto 申请信息
     * @return response
     */
    @PostMapping("/request")
    public ResponseResult<String> generateRequest(@RequestBody TradeRequestDto tradeRequestDto){
        requestService.addRequestByTradeIds(tradeRequestDto);
        return new ResponseResult<>(StatusCode.OK, "添加成功");
    }

    /**
     * 获取采购列表(务必在ps为1的情况下调用)
     * @param tradePageDto 分页条件
     * @return response
     */
    @GetMapping("/trade")
    public ResponseResult<PageVo<TradeForThirdPageVo>> getTrades(TradePageDto tradePageDto){
        PageVo<TradeForThirdPageVo> pageForThirdPage = tradeService.getPageForThirdPage(tradePageDto);
        return new ResponseResult<>(StatusCode.OK, "查询成功", pageForThirdPage);
    }

    /**
     * 分页查询付款申请(queryType=2)
     * @param requestPageDto 分页条件
     * @return response
     */
    @GetMapping("/request")
    public ResponseResult<PageVo<? extends RequestVo>> getRequestPage(RequestPageDto requestPageDto){
        PageVo<? extends RequestVo> page = null;
        if (requestPageDto.getQueryType() == RequestQueryTypeConstant.STAMP_DUTY){
            page = requestService.getPageForStampDuty(requestPageDto);
        } else if (requestPageDto.getQueryType() == RequestQueryTypeConstant.TRADE) {
            page = requestService.getPageForTrade(requestPageDto);
        }
        return new ResponseResult<>(StatusCode.OK, "查询成功", page);
    }

    /**
     * 合同扫描件上传
     * @param multipartFile 上传的文件
     * @param id 合同id
     * @return 上传结果
     */
    @PostMapping("/image/{id}")
    public ResponseResult<String> uploadTradeImage(@RequestParam("file") MultipartFile multipartFile, @PathVariable("id") Integer id){
        String url = tradeService.uploadTradeImage(id, multipartFile);
        return new ResponseResult<>(StatusCode.OK, "上传成功", url);
    }
}
